home *** CD-ROM | disk | FTP | other *** search
/ AP Professional Graphics CD-ROM Library / AP Professional Graphics CD-ROM Library.iso / pc / unix / appendix / gemsiii / parelarc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-17  |  2.0 KB  |  53 lines

  1. /*****************************************************************
  2. Plot a series of points along a PI/2-radian arc of an ellipse.
  3. The arc is specified in terms of a control polygon (a triangle)
  4. with vertices P, Q and K.  The arc begins at P, ends at Q, and is
  5. completely contained within the control polygon.  The draw_point
  6. function plots a single pixel at display coordinates (x,y).
  7.  
  8. Entry:
  9.   xP, yP, xQ, yQ, xK, yK -- coordinates of P, Q and K.  These
  10.     are 32-bit fixed-point values with 16 bits of fraction.
  11.   m -- nonnegative integer that controls spacing between points.
  12.     The angular increment between points is 1/2^m radians.
  13. Exit:
  14.   The number of points plotted is 1 + floor((PI/2)*2^m).
  15. *****************************************************************/
  16.  
  17. #define PIV2  102944     /* fixed point PI/2 */
  18. #define TWOPI 411775     /* fixed point 2*PI */
  19. #define HALF  32768      /* fixed point 1/2 */
  20. typedef long FIX;        /* 32-bit fixed point, 16-bit fraction */
  21.  
  22. qtr_elips(xP, yP, xQ, yQ, xK, yK, m)
  23. FIX xP, yP, xQ, yQ, xK, yK;
  24. int m;
  25. {
  26.     int i, x, y;
  27.     FIX vx, ux, vy, uy, w, xJ, yJ;
  28.  
  29.     vx = xK - xQ;                 /* displacements from center */
  30.     ux = xK - xP;
  31.     vy = yK - yQ;
  32.     uy = yK - yP;
  33.     xJ = xP - vx + HALF;          /* center of ellipse J */
  34.     yJ = yP - vy + HALF;
  35.     ux -= (w = ux >> (2*m + 3));  /* cancel 2nd-order error */
  36.     ux -= (w >>= (2*m + 4));      /* cancel 4th-order error */
  37.     ux -= w >> (2*m + 3);         /* cancel 6th-order error */
  38.     ux += vx >> (m + 1);          /* cancel 1st-order error */
  39.     uy -= (w = uy >> (2*m + 3));  /* cancel 2nd-order error */
  40.     uy -= (w >>= (2*m + 4));      /* cancel 4th-order error */
  41.     uy -= w >> (2*m + 3);         /* cancel 6th-order error */
  42.     uy += vy >> (m + 1);          /* cancel 1st-order error */
  43.     for (i = (PIV2 << m) >> 16; i >= 0; --i) {
  44.         x = (xJ + vx) >> 16;
  45.         y = (yJ + vy) >> 16;
  46.         draw_point(x, y);
  47.         ux -= vx >> m;
  48.         vx += ux >> m;
  49.         uy -= vy >> m;
  50.         vy += uy >> m;
  51.     }
  52. }
  53.